--[[
These three nodes could probably be registered with a single function, maybe in the future I'll code that.

]]

minetest.register_node('tasks:example_setup', { --This is the node that can be placed.
   description = 'Example node setup',
   tiles = {name='task_1.png'},
   groups = {breakable=1},
   light_source = 2,
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      meta:set_string('infotext', 'Unconfigured task')
      meta:set_string('formspec', tasks.formspec_configuration)
      meta:set_string('xp', '')
      meta:set_string('timer', '')
   end,
   on_receive_fields = function(pos, forname, fields, sender)
      local meta = minetest.get_meta(pos)
      if fields ['save'] then
         if not fields.input or fields.input == "" then
            return
         end
         local input_text = fields.input:split(', ')
         meta:set_string('xp', input_text[1])
         meta:set_string('timer', input_text[2])
         meta:set_string('infotext', input_text[3])
         meta:set_string('formspec', '')
         local node = minetest.get_node(pos)
         minetest.swap_node(pos, {name = 'tasks:example_1', param2 = node.param2}) --Swap to the active node.
      end
   end,
})

minetest.register_node('tasks:example_0', { --This node is inactive, so somebody completed the task.
   description = 'example',
   tiles = {name='task_1.png'},
   groups = {breakable=1, not_in_creative_inventory=1},
   light_source = 14,
   drop = 'tasks:example_setup',
   on_timer = function(pos)
      local this_node = minetest.get_node(pos)
      minetest.swap_node(pos, {name = 'tasks:example_1', param2 = this_node.param2})
   end,
   on_punch = function(pos)
      local meta = minetest.get_meta(pos)
      local timer_duration = tonumber(meta:get_string('timer')) or 120
      local timer = minetest.get_node_timer(pos)
      timer:start(timer_duration*2) --Make the player wait twice as long if they try to do a task that isn't available.
   end,
})

minetest.register_node('tasks:example_1', { --This node is waiting for somebody to come along and complete the task.
   description = 'example',
   tiles = {name='task_1.png'},
   groups = {breakable=1, not_in_creative_inventory=1},
   light_source = 2,
   drop = 'tasks:example_setup',
   on_punch = function(pos, node, puncher, pointed_thing)
      tasks.add_xp(pos, node, puncher, 'tasks:example_0') --Everything is pulled from the node meta expect for what node to swap to.
   end
})
